<template>
  <div class="app-container">
    <el-card shadow="never">
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="dataForm"
          size="small"
          label-width="140px"
          @keyup.enter.native="getDataList()"
        >
          <el-form-item label="广告名称：">
            <el-input
              v-model="dataForm.keyword"
              class="input-width"
              placeholder="广告名称"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button @click="getDataList()">查询</el-button>
            <el-button type="success" @click="resetData()">重置</el-button>
            <el-button type="primary" @click="addOrUpdateHandle()"
              >新增</el-button
            >
            <el-button
              type="danger"
              @click="deleteHandle()"
              :disabled="dataListSelections.length <= 0"
              >批量删除</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <el-table
      :data="list"
      style="width: 100%; margin-top: 10px"
      @selection-change="selectionChangeHandle"
      v-loading="listLoading"
      border
    >
      <el-table-column
        type="selection"
        width="60"
        align="center"
      ></el-table-column>
      <el-table-column label="编号" width="120" align="center" prop="id">
      </el-table-column>
      <el-table-column label="广告名称" align="center" prop="name">
      </el-table-column>
      <el-table-column
        label="广告位置"
        width="180"
        align="center"
        prop="status"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.type == 0">PC首页轮播</span>
          <span v-else-if="scope.row.type == 1">APP首页轮播</span>
        </template>
      </el-table-column>
      <el-table-column label="广告图片" width="180" align="center" prop="pic">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 60px"
            :src="scope.row.pic"
            fit="fill"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="上线/下线" width="120" align="center">
        <template slot-scope="scope">
          <el-switch
            @change="handleUpdateStatus(scope.row)"
            :active-value="1"
            :inactive-value="0"
            v-model="scope.row.status"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="addOrUpdateHandle(scope.row.id)"
            >编辑
          </el-button>
          <el-button size="mini" type="text" @click="deleteHandle(scope.row.id)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>

    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[8, 20, 50, 100]"
      :page-size="pageSize"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next,sizes, jumper"
    ></el-pagination>
  </div>
</template>
<script>
import AdvertiseApi from "@/api/marketing/advertise";
import AddOrUpdate from "./add-or-update";
import { pageCompose } from "@/components/mixin";

export default {
  name: "HomeAdvertiseList",
  components: {
    AddOrUpdate,
  },
  mixins: [pageCompose],
  data() {
    return {
      list: [],
      dataForm: {},
      pageIndex: 1,
      pageSize: 8,
      total: 0,
      listLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
    };
  },
  created() {
    this.getDataList();
  },
  methods: {
    resetData() {
      this.dataForm = {};
      this.getDataList();
    },
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
      this.$confirm(`确定进行删除操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          AdvertiseApi.deleteAdvertise(ids).then(({ data }) => {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          });
        })
        .catch(() => {});
    },
    handleUpdateStatus(row) {
      this.$confirm("是否要修改上线/下线状态?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          AdvertiseApi.updateStatus({ id: row.id, status: row.status }).then(
            (response) => {
              this.getDataList();
              this.$message({
                type: "success",
                message: "修改成功!",
              });
            }
          );
        })
        .catch(() => {});
    },
    getDataList() {
      this.listLoading = true;
      AdvertiseApi.getDataList({
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
        ...this.dataForm,
      }).then((response) => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.totalCount;
      });
    },
  },
};
</script>
<style scoped>
.input-width {
  width: 203px;
}
</style>
